﻿using GalaSoft.MvvmLight;
using System.Linq;
using ShoppingList.Model;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using ShoppingListEntities;
using GalaSoft.MvvmLight.Command;
using System;
using System.Globalization;
using System.Threading;
namespace ShoppingList.ViewModel
{
  
    public class StatisticsViewModel : ViewModelBase
    {
        private ShoppingListDataContext dataContext;
        public StatisticsViewModel()
        {
            dataContext = new ShoppingListDataContext(Helpers.DataBaseHelpers.Cnn);
            Options = new ObservableCollection<string>();

            Options.Add("Esta semana");
            Options.Add("Este mes");
            Options.Add("Este año");
            SetupCommands();
            CalculateThisWeek();
        }
        
        private ObservableCollection<string> _Options;
        public ObservableCollection<string> Options
        {
            get { return _Options; }
            set
            {
                _Options = value;
                RaisePropertyChanged("Options");
            }
        }

        private string _SelectedOption;
        public string SelectedOption
        {
            get { return _SelectedOption; }
            set
            {
                _SelectedOption = value;
                RaisePropertyChanged("SelectedOption");
                SelectStatisticsCommand.Execute(SelectedOption);
            }
        }

        private string _TitleChart;
        public string TitleChart
        {
            get { return _TitleChart; }
            set
            {
                _TitleChart = value;
                RaisePropertyChanged("TitleChart");
            }
        }
        private ObservableCollection<ChartValueItem> _MyValueItems;
        public ObservableCollection<ChartValueItem> MyValueItems
        {
            get { return _MyValueItems; }
            set
            {
                _MyValueItems = value;
                RaisePropertyChanged("MyValueItems");
            }
        }

        #region Commands

        public RelayCommand<string> SelectStatisticsCommand { get; set; }

        void SetupCommands()
        {
            SelectStatisticsCommand = new RelayCommand<string>((str) =>
            {
                if (str == "Esta semana")
                {
                    CalculateThisWeek();
                }
                if (str == "Este mes")
                {
                    CalculateThisMonth();
                }
                if (str == "Este año")
                {
                    CalculateThisYear();
                }
            
            });
        }

        private void CalculateThisWeek() 
        {

            this.TitleChart = "Gasto cada dia de esta semana.";
            this.MyValueItems = new ObservableCollection<ChartValueItem>();
            DateTime StartOfWeek = Helpers.TimeHelpers.GetStartOfCurrentWeek();// StartOfWeek= StartOfWeek.AddDays(1);
            DateTime EndOfWeek = Helpers.TimeHelpers.GetEndOfCurrentWeek();// EndOfWeek= EndOfWeek.AddDays(1);

       
           

            var accounts = dataContext.ShoppingLists;

            List<ShoppingListEntities.ShoppingList> list = accounts.ToList();

            var query = list.Where(x => DateTime.Parse(x.ShoppingDate) >= StartOfWeek && DateTime.Parse(x.ShoppingDate)<= EndOfWeek);

           
            foreach (var item in query)
            {
                ChartValueItem chartitem = new ChartValueItem();
                double count=0.00;
                DateTime thedate = DateTime.Parse(item.ShoppingDate);

                CultureInfo es = Thread.CurrentThread.CurrentCulture;

                chartitem.StringDate = es.DateTimeFormat.DayNames[(int)thedate.DayOfWeek];
           

                var shoppingproducts = dataContext.ShoppingListProducts
                    .Where(x => x.ShoppingListId == item.ShoppingListId);
               
                foreach (var shop in shoppingproducts)
                {
                    

                    var elements = dataContext.ProductElements.Where(x => x.ProductId == shop.ProductId && x.ShoppingListId == shop.ShoppingListId);


                    foreach (var element in elements)
                    {
                        count += element.Cost;
                    }
                     
                }

                chartitem.result = count;
                MyValueItems.Add(chartitem);
            }


        }

        private void CalculateThisMonth()
        {
            TitleChart = "Gasto de cada dia en este mes.";

            this.MyValueItems = new ObservableCollection<ChartValueItem>();
            DateTime StartOfMonth = Helpers.TimeHelpers.GetFirstDayOfMonth(DateTime.Now);
            DateTime EndOfMonth = Helpers.TimeHelpers.GetLastDayOfMonth(DateTime.Now);




            var accounts = dataContext.ShoppingLists;

            List<ShoppingListEntities.ShoppingList> list = accounts.ToList();

            var query = list.Where(x => DateTime.Parse(x.ShoppingDate) >= StartOfMonth && DateTime.Parse(x.ShoppingDate) <= EndOfMonth);


            foreach (var item in query)
            {
                ChartValueItem chartitem = new ChartValueItem();
                double count = 0.00;
                DateTime thedate = DateTime.Parse(item.ShoppingDate);
                chartitem.StringDate = thedate.ToShortDateString();


                var shoppingproducts = dataContext.ShoppingListProducts
                    .Where(x => x.ShoppingListId == item.ShoppingListId);

                foreach (var shop in shoppingproducts)
                {


                    var elements = dataContext.ProductElements.Where(x => x.ProductId == shop.ProductId && x.ShoppingListId == shop.ShoppingListId);


                    foreach (var element in elements)
                    {
                        count += element.Cost;
                    }

                }

                chartitem.result = count;
                MyValueItems.Add(chartitem);
            }
        }

        private void CalculateThisYear()
        {
            TitleChart = "Gasto cada mes del año.";

            this.MyValueItems = new ObservableCollection<ChartValueItem>();
            for (int i = 0; i < 12; i++)
            {
              
                DateTime StartOfMonth = Helpers.TimeHelpers.GetFirstDayOfMonth(i+1);
                DateTime EndOfMonth = Helpers.TimeHelpers.GetLastDayOfMonth(i+1);

                 ChartValueItem chartitem = new ChartValueItem();
                    double count = 0.00;
                    chartitem.StringDate = Helpers.TimeHelpers.months[i]; 
                   

                var accounts = dataContext.ShoppingLists;

                List<ShoppingListEntities.ShoppingList> list = accounts.ToList();

                var query = list.Where(x => DateTime.Parse(x.ShoppingDate) >= StartOfMonth && DateTime.Parse(x.ShoppingDate) <= EndOfMonth);


                foreach (var item in query)
                {
                    DateTime thedate = DateTime.Parse(item.ShoppingDate);

                  
                    var shoppingproducts = dataContext.ShoppingListProducts
                        .Where(x => x.ShoppingListId == item.ShoppingListId);

                    foreach (var shop in shoppingproducts)
                    {


                        var elements = dataContext.ProductElements.Where(x => x.ProductId == shop.ProductId && x.ShoppingListId == shop.ShoppingListId);


                        foreach (var element in elements)
                        {
                            count += element.Cost;
                        }

                    }

                    chartitem.result = count;
                   
                }

                MyValueItems.Add(chartitem);








            }
        }
        #endregion
    }
}